package demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;

public class PlayCard {
    //在这里类里面操作牌：设置牌，洗牌和发牌
    public static final String[] suits = {"♠","♥","♣","♦"};//定义花色的数组

    //1.设置牌操作
    public List<Card> setCard() {
        List<Card> cards = new ArrayList<>();//定义一个数组，用来存放所有牌
        for (int i = 0; i < 4; i++) {//四种花色
            for (int j = 1; j <=13 ; j++) {//13个号码
                String suit = suits[i];//获得一个花色

                Card card = new Card(suit,j);//定义一个牌对象并赋值

                cards.add(card);//将牌放入一个牌数组中
            }
        }
        return cards;//返回一副牌
    }

    //2.洗牌操作
    public void shuffle(List<Card> cards) {
        Random random = new Random();
        for (int i = 51; i > 0; i--) {
            int index  = random.nextInt(i);//随机获取一个牌下标与i下标的牌交换
            swap(cards,i,index);
        }
        //return cards;
    }
    private static void swap(List<Card> cards,int i,int j) {
        //交换两张牌
        Card tmp = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }

    //3.发牌操作
    public List<List<Card>> getCard(List<Card> cards) {
        //定义三个对象，存取牌
        List<Card> hand1 =new ArrayList<>();
        List<Card> hand2 =new ArrayList<>();
        List<Card> hand3 =new ArrayList<>();
        //定义一个二维数组，存取三个对象
        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //每个人只能拿五张牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
              Card card = cards.remove(0);//每次拿到第一张牌，并从数组中删掉
              //放入每个人手中
              hand.get(j).add(card);
            }
        }
        return hand;
    }
}
